package com.quectel.cms.controller.parking;


import com.quectel.business.common.util.sys.SysCodeUtils;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.sys.SysCodeKeyConstants;
import com.quectel.core.module.parking.dto.ParkingCurrentCarDto;
import com.quectel.core.module.parking.service.ParkingCurrentCarService;
import com.quectel.util.common.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/04/13 11:16
 */
@RestController
@RequestMapping("parkingCurrentCar")
@Tag(name = "ParkingCurrentCarController", description = "停车场在停车辆相关api")
public class ParkingCurrentCarController extends BaseController {

    @DubboReference
    private ParkingCurrentCarService parkingCurrentCarService;

    /**
     * 列表
     */
    @GetMapping("exportExcel")
    @RequirePermissions(values = "parkingCurrentCar:exportExcel")
    public void exportExcel(@RequestParam Map<String, Object> params, HttpServletResponse httpServletResponse) {
        Request request = Request.configParamsNotPage(params);

        super.fillAuthorityData(params);

        List<ParkingCurrentCarDto> result = new ArrayList<>();
        int pageNo = 0;
        params.put("limit", 1000);
        while (true) {
            params.put("offset", pageNo++ * 1000);
            List<ParkingCurrentCarDto> list = parkingCurrentCarService.queryList(params);
            if (org.apache.commons.collections4.CollectionUtils.isEmpty(list)) {
                break;
            }
            result.addAll(list);
            if (list.size() < 1000) {
                break;
            }
        }
        String[] headers = new String[]{"所属小区", "车牌号", "车牌颜色", "购卡情况", "所属人姓名", "所属人电话", "停车场名称", "入场时间"};
        List<String[]> dataList = new ArrayList<>();
        for (ParkingCurrentCarDto data : result) {
            String[] item = new String[headers.length];
            item[0] = Objects.nonNull(data.getVillage()) ? data.getVillage().getName() : "";
            item[1] = data.getLicense();
            item[2] = SysCodeUtils.easyToGetName(SysCodeKeyConstants.LICENSE_COLOR, data.getLicenseColor() + "");
            item[3] = SysCodeUtils.easyToGetName(SysCodeKeyConstants.PARKING_IS_PAY_CARD, data.getIsPayCard() + "");
            item[4] = data.getOwnerName();
            item[5] = data.getOwnerMobile();
            item[6] = data.getParkName();
            item[7] = data.getInTime() != null ? DateUtils.format(data.getInTime(), DateUtils.FormatType.COMMON) : null;
            dataList.add(item);
        }
        POIUtils.exportExcel(httpServletResponse, "在停车辆信息", headers, dataList, "在停车辆信息");

    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "parkingCurrentCar:list")
    @Operation(summary = "分页列表")
    public Response<Page<ParkingCurrentCarDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = parkingCurrentCarService.queryTotal(request.getParams());

        List<ParkingCurrentCarDto> list = null;
        if (total > 0) {
            list = parkingCurrentCarService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<ParkingCurrentCarDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<ParkingCurrentCarDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "parkingCurrentCar:edit")
    @Operation(summary = "查询单条记录")
    public Response<ParkingCurrentCarDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (parkingCurrentCarService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        ParkingCurrentCarDto parkingCurrentCarDto = parkingCurrentCarService.selectById(id);

        return Response.<ParkingCurrentCarDto>ok().wrap(parkingCurrentCarDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "parkingCurrentCar:info")
    @Operation(summary = "查询单条记录")
    public Response<ParkingCurrentCarDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (parkingCurrentCarService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        ParkingCurrentCarDto parkingCurrentCarDto = parkingCurrentCarService.selectById(id);

        return Response.<ParkingCurrentCarDto>ok().wrap(parkingCurrentCarDto);
    }

    /**
     * 保存
     *
     * @param parkingCurrentCarDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "parkingCurrentCar:edit")
    @Operation(summary = "保存单条记录")
    public Response<Object> save(@RequestBody @Validated ParkingCurrentCarDto parkingCurrentCarDto) {

        //填充基础数据
        fillSaveCommonData(parkingCurrentCarDto);

        parkingCurrentCarService.save(parkingCurrentCarDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param parkingCurrentCarDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "parkingCurrentCar:edit")
    @Operation(summary = "修改单条记录")
    public Response<Object> update(@RequestBody @Validated ParkingCurrentCarDto parkingCurrentCarDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(parkingCurrentCarDto.getId()));
        if (parkingCurrentCarService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(parkingCurrentCarDto);

        parkingCurrentCarService.updateById(parkingCurrentCarDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "parkingCurrentCar:delete")
    @Operation(summary = "批量删除记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (parkingCurrentCarService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        parkingCurrentCarService.deleteBatch(ids);

        return Response.ok();
    }
}
